package ch.rheintalconnection.sysventory.technicalServices.wmi;

import ch.rheintalconnection.sysventory.dto.LogicalDiskDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class LogicalDiskScan {

	private ActiveXComponent axWMI;

	public static final String QUERY_LOGICALDISK = "SELECT * FROM Win32_LogicalDisk";

	public LogicalDiskScan(ActiveXComponent axWMI) {
		this.axWMI = axWMI;
	}

	public LogicalDiskDto scan() {

		LogicalDiskDto logicalDiskDto = null;

		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY_LOGICALDISK));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;

		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String hdcapacity = Dispatch.call(item, "Size").toString();
			String freespace = Dispatch.call(item, "FreeSpace").toString();
			
			logicalDiskDto = new LogicalDiskDto(hdcapacity, freespace);
			return logicalDiskDto;
		}
		
		return null;
	}
}
